<template>
  <pro-table
    ref="tableRef"
    :columns="columns"
    :fetch="fetch"
    :toolbar="toolbar"
    :operate="operate"
  ></pro-table>
  <DesignAddEdit
    :visible="visibleOfDia"
    @close="closeDia"
    :editData="formData"
  ></DesignAddEdit>
  <!-- 删除对话框 -->
  <a-modal
    :visible="deleteVisible"
    title="温馨提示"
    @ok="handleOkModal"
    @cancel="handleCancelModal"
  >
    确认删除吗？
  </a-modal>
</template>

<script setup>
import global from "../../global/global.vue";
import { ref } from "vue";
import DesignAddEdit from "./components/DesignAddEdit";
import { sjbgList, sjbgDeleteById } from "@/api/module/constructAndImplement";
import { message } from "ant-design-vue";

// 项目id
const projectId = global.xmid;

// 控制新增弹窗
const visibleOfDia = ref(false);
const closeDia = function () {
  visibleOfDia.value = false;
  tableRef.value.reload();
};

const tableRef = ref();
const columns = [
  {
    title: "序号",
    align: "center",
    customRender: ({ text, record, index }) => index + 1,
    width: 60,
  },
  { dataIndex: "time", title: "变更时间" },
  { dataIndex: "bgnr", title: "变更内容" },
  { dataIndex: "dtzzcdyx", title: "对投资造成的影响" },
  { dataIndex: "pfdw", title: "批复单位" },
  {
    dataIndex: "bz",
    title: "备注",
  },
  {
    dataIndex: "lx",
    title: "类型",
    conver: {
      0: "重大",
      1: "一般",
    },
  },
  {
    dataIndex: "pfFileName",
    title: "批复文件",
    document: "pfFileName",
    domainName: "/api/project/constructionImplementation/sjbgDownload",
    documentId: "pfFile",
  },
];

const fetch = async () => {
  const res = await sjbgList({ id: projectId });
  console.log(res);
  return {
    data: res.data,
  };
};

/// 工具栏
const toolbar = [
  {
    label: "新增",
    event: function () {
      visibleOfDia.value = true;
    },
  },
];

const formData = ref({
  bgnr: "",
  time: null,
  dtzzcdyx: "",
  pfdw: "",
  bz: "",
  lx: "",
  pfFileName: "",
  isEdit: false,
});

const deleteVisible = ref(false);
/// 行操作
const deleteId = ref("");
const operate = [
  {
    label: "编辑",
    event: function (record) {
      formData.value = record;
      formData.value.isEdit = true;
      visibleOfDia.value = true;
    },
  },
  {
    label: "删除",
    event: function (record) {
      deleteId.value = record.id;
      deleteVisible.value = true;
    },
  },
];

// 删除确认
const handleOkModal = async () => {
  const res = await sjbgDeleteById({
    id: deleteId.value,
  });
  message.success("删除成功");
  tableRef.value.reload();
  deleteVisible.value = false;
};
const handleCancelModal = () => {
  deleteVisible.value = false;
};
</script>

<style lang="scss" scoped></style>
